<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text">
    <script>
        const inp = document.querySelector('input');
        inp.addEventListener('input', debounce(search,1000));
        function search () {
            console.log(inp.value);
        }
        // 防抖
        // 输入完成查询一次
        // function debounce (fn) {
        //     let timer;
        //     return function () {
        //         if (timer) {
        //             clearTimeout(timer);
        //         }
        //         timer = setTimeout(() => {
        //             // 执行外部逻辑
        //             fn();
        //         },1000);
        //     }
        // }
        
        function debounce(fn, delay) {
            let timer;
            return function (...args) {
                timer && clearTimeout(timer);
                timer = setTimeout(() => {
                    fn.apply(this, args);
                }, delay)
            };
        }
        function run() {
            console.log('123');
        }
        const debounceRun = debounce(run, 1000);
    </script>
</body>
</html>